/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Services;

import Exceptions.NonexistentEntityException;
import entitys.Appointment;
import entitys.MedicalRecord;
import entitys.Vacine;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaQuery;

/**
 *
 * @author EdwinHernan
 */
public class MedicalRecordService{
    public MedicalRecordService(EntityManagerFactory emf){
        this.emf=emf;  
        VacineService vacineService =new VacineService(emf);
        AppointmentService appointmentService= new AppointmentService(emf);
    }
    private EntityManagerFactory emf=null;
    VacineService vacineService=null;
    AppointmentService appointmentService=null;
    
    public EntityManager getEntityManager(){
        return emf.createEntityManager();
    }
    
    public void create(MedicalRecord medicalrecord) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(medicalrecord);
            em.getTransaction().commit();
        }finally {
            if (em != null){
                em.close();
            }
        }
    }
    
    public void edit(MedicalRecord medicalrecord) throws NonexistentEntityException, Exception {
        EntityManager em=null;
        try{
            em=getEntityManager();
            em.getTransaction().begin();
            em.merge(medicalrecord);
            em.getTransaction().commit();  
        }catch(Exception ex){
            String msg = ex.getLocalizedMessage();
            if(msg==null || msg.length()==0){
                Long id=medicalrecord.getId();
                //if(findMedicalRecord(id)==null){
                    //throw new NonexistentEntityException("The Medical Record with id" +id+"no longer exists.");
                //}
            }
            throw ex;
        }finally{
            if(em !=null){
                em.close();
            }
        }
        
    }
    public void destroy(Long id) throws NonexistentEntityException{
        EntityManager em=null;
        try{
            em=getEntityManager();
            em.getTransaction().begin();
            MedicalRecord medicalrecord;
            try{
                medicalrecord=em.getReference(MedicalRecord.class, id);
                medicalrecord.getId();
            }catch(EntityNotFoundException enfe){
                throw new NonexistentEntityException("the Medical Record with id "+id+"no longer exist.",enfe);
            }
            em.remove(medicalrecord);
            em.getTransaction().commit();
        }finally{
            if(em!=null){
                em.close();
            }
        }
    }
    public List<MedicalRecord> findMedicalRecordEntities(){
        return findMedicalRecordEntities(true,-1,-1);
    }
    public List<MedicalRecord> findMedicalRecordEntities(int maxResults, int firstResult){
        return findMedicalRecordEntities(false,maxResults,firstResult);
    }
    private List<MedicalRecord>findMedicalRecordEntities(boolean all,int maxResults,int firstResult){
        EntityManager em=getEntityManager();
        try{
            CriteriaQuery cq=em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(MedicalRecord.class));
            Query q=em.createQuery(cq);
            if((!all)){
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            for(int i=0;i<q.getResultList().size();i++){
            MedicalRecord medicalrecord=(MedicalRecord) q.getResultList().get(i);
            Long medicalrecordId=medicalrecord.getId();
            Query query=em.createNamedQuery("select vacineList_idvacine from medicalrecord_vacine"+"where Vacine_idvacine="+medicalrecordId);
            //medicalrecord.setvacine(findVacine(query));
            }
            for(int i=0;i<q.getResultList().size();i++){
            Appointment appointment=(Appointment) q.getResultList().get(i);
            Long appointmentId=appointment.getIdAppointment();
            Query query=em.createNamedQuery("select appointment_IdAppointment from medicalrecord_appointment"+"where Appointment_IdAppointment="+appointmentId);
            //appointment.setIdAppointment(findAppointment(query));
            }
            
            return q.getResultList();
        }finally{
            em.close();
        }
    }

    
}
